﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using LDSP.BRL;
using System.Drawing;

namespace LDSP.UI.Admin
{
    public partial class ReportCodification : LDSP.BRL.BaseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (this.RB())
                {
                    this.InitDataDynamicBefore();
                    if (!IsPostBack)
                    {
                        this.InitData();
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "name";
            g_pageInfo.PageSize = 10;
            //g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }
        protected bool RB()
        {
            return this.AllowSystemUser();
        }
        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.BindDDLDownloadRank();
            this.BindDDLPeriod();
            this.SetSpecialPageInfo();
            this.CreateReportInfo();
        }
        protected void InitDataDynamicBefore()
        {
        }

        protected void BindDDLDownloadRank()
        {
            this.ddl_downloadRank.Items.Clear();
            this.ddl_downloadRank.Items.Add("10");
            this.ddl_downloadRank.Items.Add("20");
            this.ddl_downloadRank.Items.Add("50");
            this.ddl_downloadRank.SelectedIndex = 0;
        }
        protected void BindDDLPeriod()
        {
            this.ddl_period.Items.Clear();
            this.ddl_period.Items.Add("一周");
            this.ddl_period.Items.Add("一月");
            this.ddl_period.Items.Add("一季");
            this.ddl_period.Items.Add("一年");
            this.ddl_period.SelectedIndex = 0;
        }

        protected void GetCondition()
        {
            string cond = "";
            g_pageInfo.Condition = cond;
        }
        protected int GetTopNumber()
        {
            return this.GetInt(this.ddl_downloadRank.SelectedValue);
        }
        protected string GetStartDT()
        {
            DateTime res = DateTime.Now;
            switch (this.ddl_period.SelectedIndex)
            {
                case 0:
                    res = res.AddDays(-7);
                    break;
                case 1:
                    res = res.AddMonths(-1);
                    break;
                case 2:
                    res = res.AddMonths(-3);
                    break;
                case 3:
                    res = res.AddYears(-1);
                    break;
            }
            return this.FormatDT(res);
        }
        protected string GetEndDT()
        {
            DateTime res = DateTime.Now;
            //switch (this.ddl_period.SelectedIndex)
            //{
            //    case 0:
            //        res = res.AddDays(-7);
            //        break;
            //    case 1:
            //        res = res.AddMonths(-1);
            //        break;
            //    case 2:
            //        res = res.AddMonths(-3);
            //        break;
            //    case 3:
            //        res = res.AddYears(-1);
            //        break;
            //}
            return this.FormatDT(res);
        }

        protected void CreateReportInfo()
        {
            ReportInfo ri = new ReportInfo();
            ri.DataSource = new DownloadLog().GetReportForDoc(this.GetTopNumber(), this.GetStartDT(), this.GetEndDT());
            ri.XName = "docName";
            ri.YName = "times";
            ri.Title = string.Format("{0}下载次数前{1}位文档排名", this.ddl_period.SelectedValue, this.ddl_downloadRank.SelectedValue);
            this.Session["SystemReport"] = ri;

            this.img_report.ImageUrl = "ReportImg.aspx?" + Guid.NewGuid().ToString();
        }
		
        protected void btn_ok_Click(object sender, EventArgs e)
        {
            try
            {
                this.CreateReportInfo();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
    }

}